<?php
/**
* @version $Id: submit.php,v 1.1 2005/10/13 13:01:01 mic Exp $
* @package XfaQ - a frequently asked question component for Joomla! & Mambo CMS
* @copyright (C) 2005 mgfi.info
* @info info@mgfi.info http://www.mgfi.info
* @license GNU/GPL
* based in simplefaq 2.0.1 & glossary 1.4
*/

# Don't allow direct linking
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

$id=mosGetParam( $_REQUEST, 'id','' );
$page=mosGetParam( $_REQUEST, 'page','1' );

global $my, $is_editor, $is_admin, $name, $email, $location, $question, $homepage, $answer, $date_created, $comment, $edit, $editdate;

// Check if Registered Users only
if( !$xfaq['anonentry'] && !$is_user ) {
    echo _XFAQ_ONLYREGISTERED;
} else { ?>
    <script type="text/javascript">
  		<!--
    	function validate(){
    		if (((<?php echo $xfaq['requestauthorname']; ?>) && (document.adminForm.name.value=='')) || (document.adminForm.question.value=='') || ((<?php echo $xfaq['answermandatory']; ?>)&&(document.adminForm.answer.value=='')) || (document.adminForm.catid.value=='0')){
    		   alert('<?php echo _XFAQ_VALIDATE; ?>');
    		} else {
    			document.adminForm.action = 'index.php';
    			document.adminForm.submit();
    		}
    	}

		function submitbutton(pressbutton) {
			var form = document.adminForm;
			if (pressbutton == 'cancel') {
                document.adminForm.task.value = 'cancel';
				submitform( pressbutton );
				return;
			}
			if (pressbutton == 'save') {
				validate();
				return;
			}
        }
    	//-->
    </script>

    <form name="adminForm" action="index.php" target="_top" method="post">
    	<table align="center" width="90%" cellpadding="0" cellspacing="4" border="0">
    		<tr>
    			<td colspan="2">
    				<?php
    				// Check if User is Admin and if he wants to edit
                    if((( $is_editor ) || ( $is_admin )) && ( $id )) {
                        echo '<input type="hidden" name="id" value="' . $id . '">';
                        $query1 = 'SELECT * FROM ' . $GLOBALS['mosConfig_dbprefix'] . 'xfaq WHERE id = \'' . $id . '\'';
                        $result1 = $database->setquery( $query1 );
                        $row = $database->loadobjectlist( $result1 );
                        $id = $row[0]->id;
                        $name = $row[0]->name;
                        $email = $row[0]->email;
                        $location = $row[0]->location;
                        $question = $row[0]->question;
                        $homepage = $row[0]->homepage;
                        $answer = $row[0]->answer;
                        $date_created = $row[0]->date_created;
                        $comment = $row[0]->comment;
                        $edit = $row[0]->edit;
                        $editdate = $row[0]->editdate;
                    }
                    // get list of categories
                    $categories[] = mosHTML::makeOption( '0', _SEL_CATEGORY );
                    $database->setQuery( 'SELECT id AS value, name AS text FROM #__categories WHERE section=\'com_xfaq\' ORDER BY ordering' );
                    $categories = array_merge( $categories, $database->loadObjectList() );

                    if( count( $categories ) < 1 ) {
                        mosRedirect( 'index2.php?option=categories&section=' . $option, _XFAQ_ERR_SEL_ADD_FIRST );
                    }

                    $clist = mosHTML::selectList( $categories, 'catid', 'class="inputbox" size="1"','value', 'text', intval( $catid ) );
                    ?>

                    <input type="hidden" name="option" value="com_xfaq" />
                    <input type="hidden" name="Itemid" value="<?php echo $Itemid; ?>" />
                    <input type="hidden" name="page" value="<?php echo $page; ?>" />
                    <input type="hidden" name="task" value="entry" />
                </td>
            </tr>
            <?php
            if($xfaq['requestauthorname'] ){ ?>
                <tr>
                    <td width="130"><?php echo _XFAQ_ENTERNAME; ?> *</td>
                    <td><input type="text" name="name" style="width:245px;" class="inputbox" value="<?php echo $name; ?>" /></td>
                </tr>
                <?php
            }else{ ?>
                <input type="hidden" name="name" value="<?php echo _XFAQ_NA; ?>" />
                <?php
            }
            if( $xfaq['requestauthoremail'] ){ ?>
                <tr>
                    <td width="130"><?php echo _XFAQ_ENTERMAIL; ?></td>
                    <td><input type="text" name="email" style="width:245px;" class="inputbox" value="<?php echo $email; ?>"></td>
                </tr>
                <?php
            }else{ ?>
                <input type="hidden" name="email" value="<?php echo _XFAQ_NA; ?>" />
                <?php
            }
            if( $xfaq['requestauthorhomepage'] ){ ?>
                <tr>
                    <td width="130"><?php echo _XFAQ_ENTERPAGE; ?></td>
                    <td><input type="text" name="homepage" style="width:245px;" class="inputbox" value="<?php echo $homepage; ?>"></td>
                </tr>
                <?php
            }
            else{ ?>
                <input type="hidden" name="homepage" value="<?php echo _XFAQ_NA; ?>" />
                <?php
            }
            if( $xfaq['requestauthorlocation'] ){ ?>
                <tr>
                    <td width="130"><?php echo _XFAQ_ENTERLOCA; ?></td>
                    <td><input type="text" name="location" style="width:245px;" class="inputbox" value="<?php echo $location; ?>"></td>
                </tr>
                <?php
            }else{ ?>
            	<input type="hidden" name="location" value="<?php echo _XFAQ_NA; ?>" />
            	<?php
            } ?>

            <tr>
                <td width="130">&nbsp;</td><td>&nbsp;</td>
            </tr>
            <tr>
                <td width="130" valign="top"><?php echo _XFAQ_CATEGORY; ?> *</td>
                <td><?php echo $clist; ?></td>
            </tr>
            <tr>
                <td width="130" valign="top"><?php echo _XFAQ_ENTERQUESTION; ?> *</td>
                <td>
                    <textarea class="inputbox" name="question" cols="50" rows="2" wrap="virtual"><?php echo $question; ?></textarea>
                </td>
            </tr>

            <?php
            // Decide whether to show the answer box
            if( $xfaq['answermandatory'] || $xfaq['requestanswer'] ) { ?>
                <tr>
                    <td width="130" valign="top">
                        <?php echo _XFAQ_ENTERANSWER;
                        if( $xfaq['answermandatory'] ) echo ' * '; ?>
                        <br /><br />
                    </td>
                    <td valign="top">
                        <textarea class="inputbox" name="answer" cols="50" rows="4" wrap="virtual"><?php echo $answer; ?></textarea>
                    </td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td style="font-size:9px; color:#999;"><?php echo _XFAQ_FIELDS_ARE_MANDATORY; ?></td>
                </tr>
                <?php
            } ?>
            <tr>
                <td>&nbsp;</td>
                <td>
                    <a href="javascript:submitbutton('save');" title="<?php echo _XFAQ_SAVE; ?>"><button type="submit" class="button"><?php echo _XFAQ_SAVE; ?></button></a>
                    &nbsp;&nbsp;
                    <a href="javascript:submitbutton('cancel');" title="<?php echo _XFAQ_CANCEL; ?>"><button type="submit" class="button"><?php echo _XFAQ_CANCEL; ?></button></a>
                </td>
            </tr>
        </table>
    </form>
    <?php
}
?>